package com.qding.park.common.core.redis.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPubSub;

public class JedisClientCluster implements JedisClient {

	@Autowired
	private JedisCluster jedisCluster;
	
	@Override
	public String get(String key) {
		return jedisCluster.get(key);
	}

	@Override
	public String set(String key, String value) {
		return jedisCluster.set(key, value);
	}

	@Override
	public String hget(String hkey, String key) {
		return jedisCluster.hget(hkey, key);
	}

	@Override
	public long hset(String hkey, String key, String value) {
		return jedisCluster.hset(hkey, key, value);
	}

	@Override
	public long incr(String key) {
		return jedisCluster.incr(key);
	}

	@Override
	public long expire(String key, int second) {
		return jedisCluster.expire(key, second);
	}

	@Override
	public long ttl(String key) {
		return jedisCluster.ttl(key);
	}

	@Override
	public long del(String key) {
		
		return jedisCluster.del(key);
	}

	@Override
	public long hdel(String hkey, String key) {
		
		return jedisCluster.hdel(hkey, key);
	}

	@Override
	public Object get(String key, Class<?> objType) {
		String string = jedisCluster.get(key);
		
		return StringUtils.isBlank(string)?null:SerializeUtils.unSerialize(string, objType);
	}

//	@Override
//	public String set(String key, Object value) {
//		String string = jedisCluster.set(key, value==null?"":SerializeUtils.serialize(value));
//		return string;
//	}

	@Override
	public String set(String key, String value, int ttl) {
		String string=this.set(key, value);
		this.expire(key, ttl);
		return string;
	}

//	@Override
//	public String set(String key, Object value, int ttl) {
//		String string = jedisCluster.set(key, value==null?"":SerializeUtils.serialize(value));
//		this.expire(key, ttl);
//		return string;
//	}

	@Override
	public long publish(String channel, String message) {
//		jedisCluster.
//		jedisCluster.p
		return 0;
	}

	@Override
	public void subscribe(JedisPubSub jedisPubSub, String... channels) {
		// TODO Auto-generated method stub
//		jedisCluster.un
	}

	@Override
	public long  lpush(final String key, final String... string) {
		// TODO Auto-generated method stub
		return jedisCluster.lpush(key, string);
	}

	@Override
	public String rpop(String key) {
		// TODO Auto-generated method stub
		return jedisCluster.rpop(key);
	}

	@Override
	public List<String> brpop(String key) {
		// TODO Auto-generated method stub
		return jedisCluster.brpop(key);
	}

	@Override
	public String getSet(String key, String value) {
		// TODO Auto-generated method stub
		return jedisCluster.getSet(key, value);
	}

	@Override
	public long setnx(String lockKey, int expireTime, String stringOfLockExpireTime) {
		// TODO Auto-generated method stub
		long i=jedisCluster.setnx(lockKey, stringOfLockExpireTime);
		if(i==1){
			jedisCluster.expire(lockKey, expireTime);
		}
		return i;
	}

	@Override
	public long setnx(String key, String value) {
		// TODO Auto-generated method stub
		return jedisCluster.setnx(key, value);
	}

	@Override
	public Map<String, String> hgetAll(String hkey) {
		return jedisCluster.hgetAll(hkey);
	}

	@Override
	public void delPattern(String key) {
		
	}

}
